package libs
{
	import flash.net.URLVariables;
	
	import mx.controls.Alert;
	import mx.rpc.events.FaultEvent;
	import mx.rpc.events.ResultEvent;
	import mx.rpc.http.HTTPService;
	
	public class httpSend
	{
		 [Bindable]public var callbackFunction:Function;  //回调函数
		 [Bindable]public var temp:XML;
		 [Bindable]public var params:URLVariables = new URLVariables();
		 [Bindable]public var getUrl:String;
		 [Bindable]public var reFormat:String="object";
	
		private var httpService:HTTPService=new HTTPService();
				public var URL:String ="http://localhost/bb/index.php?Controller=";//网站请求地址

		public function httpSend()
		{
			super();
		}
		
		public function send():void{
			httpService.url=getUrl;
			httpService.method="POST";
			httpService.resultFormat=this.reFormat;
			httpService.send(params);
			httpService.addEventListener(ResultEvent.RESULT,complete);
		    httpService.addEventListener(FaultEvent.FAULT,fault);
		}
		public function fault(e:FaultEvent):void{
			
			
			Alert.show("httpFault:"+e.toString());
			this.dispatchEvent(new Event("COMPLATE",true));

			//Alert.show("网络连接可能出了小小问题，请刷新浏览器再试试看:)！");
		}
		public function complete(e:ResultEvent):void{
			//trace(httpService.resultFormat);
		// temp=new XML(httpService.lastResult);	
		
		  callbackFunction.call(this,e);
		}
		public function httpEncoding(param:String):String{
                return param;//encodeURI(param);
            }
		
			/**
		 * 数据交换函数，用于跟FleaPHP交互用
		 * controller 请求的控制器
		 * action 请求的动作
		 * params 参数
		 * callBackFun 回调函数 
		 * 
		 * */
		public function dataSend(controller:String,action:String,paramsArray:URLVariables,callBackFun:Function):void
			{
				
		
					this.params = paramsArray;
                    this.params.action=httpEncoding(action);
                   
      				this.callbackFunction = callBackFun;
					this.getUrl=URL+controller+"&action="+action+"&rnd="+Math.random();
					this.send();
					
              }
              
	}
}